<template>
  <div class="channel-edit__container">
    <!-- 弹出层的头部区域 -->
    <van-nav-bar title="频道管理">
      <template #right>
        <van-icon name="cross" size="0.37333334rem" color="white" @click="closePup"/>
      </template>
    </van-nav-bar>
    <!-- 我的频道 -->
    <div class="my-channel-box">
      <div class="channel-title">
        <span>我的频道
          <span class="small-title">
            点击{{isShow?"进入":"删除"}}频道
          </span>
        </span>
        <span @click="isShow=!isShow">{{isShow?"完成":"编辑"}}</span>
      </div>
      <!-- 我的频道列表 -->
      <van-row type="flex">
        <van-col span="6" v-for="(obj,index) in channelsArticleList" :key="obj.id"  @click="$emit('switch-channel', obj.id)">
          <div class="channel-item van-hairline--surround">
            {{obj.name}}
            <!-- 删除的徽标 -->
            <van-badge color="transparent" class="cross-badge" v-if="obj.id > 0 && isShow ">
              <template #content>
                <van-icon
                  name="cross"
                  class="badge-icon"
                  color="#cfcfcf"
                  size="0.32rem"
                  @click="$emit('del-channel',index)"
                />
              </template>
            </van-badge>
          </div>
        </van-col>
      </van-row>
    </div>

    <!-- 更多频道 -->
    <div class="more-channel-box">
      <div class="channel-title">
        <span>点击添加更多频道：</span>
      </div>
      <!-- 更多频道列表 -->
      <van-row type="flex">
        <van-col span="6" v-for="obj in shaixaun" :key="obj.id" @click="addChennels(obj)">
          <div class="channel-item van-hairline--surround">{{obj.name}}</div>
        </van-col>
      </van-row>
    </div>
  </div>
</template>

<script>
import { ArticleAllchannels } from '@/api/home'
export default {
  name: 'channel',
  methods: {
    // 点击将当前对象添加到父组件数组中
    addChennels (obj) {
      // 判断是否点击编辑按钮
      if (!this.isShow) {
        this.$toast('请先点击编辑模式哈!')
        return
      }
      //  要更改父组件中的数据 就交给父组件去修改就好
      this.$emit('add-channelFn', obj)
    },
    // 点击关闭弹出层的同时将选中编辑状态下的徽标手动改为false
    closePup () {
      this.$emit('click')
      setTimeout(() => {
        this.isShow = false
      }, 500)
    }
  },
  data () {
    return {
      //  来控制 编辑状态或是删除状态
      isShow: false,
      // 接收所有的列表选项
      channelsList: []
    }
  },
  // 父传子接 在自定义组件中拿到已经有的数据列表
  props: {
    channelsArticleList: {
      type: Array,
      required: true
    }
  },
  // 先拿到全部的列表选项
  async created () {
    const resp = await ArticleAllchannels()
    this.channelsList = resp.data.channels
  },
  // 通过计算属性依耐变量得到的新的数据
  computed: {
    shaixaun () {
      return this.channelsList.filter(obj => {
        // 其实这种简写形式就相当于将查找出来的结果去反值 还是只因filter只会留下true的不要false
        return !this.channelsArticleList.find(item => item.id === obj.id)
        // // 判断出的当前来数组不相同的值
        // if (this.channelsArticleList.find(item => item.id === obj.id)) {
        //   // 因为failter只是留下为true 过滤掉为false的
        //   return false
        // } else {
        //   return true
        // }
      })
    }
  }
}
</script>

<style scoped lang="less">
.van-popup,
.popup-container {
  background-color: transparent;
  height: 100%;
  width: 100%;
}

.popup-container {
  display: flex;
  flex-direction: column;
}

.pop-header {
  height: 90px;
  background-color: #007bff;
  color: white;
  text-align: center;
  font-size: 14px;
  position: relative;
  .title {
    width: 100%;
    position: absolute;
    bottom: 15px;
  }
}

.pop-body {
  flex: 1;
  overflow: scroll;
  padding: 8px;
  background-color: white;
}

.my-channel-box,
.more-channel-box {
  .channel-title {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    line-height: 28px;
    padding: 0 6px;
  }
}

.channel-item {
  font-size: 12px;
  text-align: center;
  line-height: 36px;
  background-color: #fafafa;
  margin: 5px;
}

/*删除的微标 */
.cross-badge {
  position: absolute;
  right: -3px;
  top: 0;
  border: none;
}

/*提示文字 */
.small-title {
  font-size: 10px;
  color: gray;
}
</style>
